package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.CustomerUser;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.RsaUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.service.*;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 */
@RestController
public class CustomerLoginController extends BaseController {
    @Autowired
    private CustomerLoginService loginService;

    @Autowired
    private CustomerRegisterService customerRegisterService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/cus/login")
    @Anonymous
    public AjaxResult login(@RequestBody LoginBody loginBody) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.customerLogin(loginBody.getUsername(), RsaUtils.decryptByPrivateKey(loginBody.getPassword()));
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/cus/getInfo")
    @Anonymous
    public AjaxResult getInfo() {
        CustomerUser user = SecurityUtils.getLoginUser().getCustomerUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }


    @PostMapping("/cus/register")
    @Anonymous
    public AjaxResult register(@RequestBody RegisterBody user) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        String msg = customerRegisterService.register(user);
        String token = loginService.customerLogin(user.getUsername(), RsaUtils.decryptByPrivateKey(user.getPassword()));
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

//    /**
//     * 退出登录
//     *
//     * @param loginBody 登录信息
//     * @return 结果
//     */
//    @PostMapping("/cus/loginout")
//    @Anonymous
//    public AjaxResult loginout(@RequestBody LoginBody loginBody) throws IOException, ServletException {
//        LoginUser loginUser = tokenService.getLoginUser(request);
//        if (StringUtils.isNotNull(loginUser)) {
//            String userName = loginUser.getUsername();
//            // 删除用户缓存记录
//            tokenService.delLoginUser(loginUser.getToken());
//            // 记录用户退出日志
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, MessageUtils.message("user.logout.success")));
//        }
//        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success(MessageUtils.message("user.logout.success"))));
//    }
//}

}
